import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import swal, { SweetAlertType } from 'sweetalert2';
import {HttpService} from "@core/services/http.service";
import {HttpClient} from "@angular/common/http";

@Component({
    selector: 'app-c-seek-tool',
    templateUrl: './c-seek-tool.component.html',
    styleUrls: ['./c-seek-tool.component.less']
})
export class CSeekToolComponent implements OnInit {

    @Output() onChange = new EventEmitter<any>();
    @Output() onChangeTxt = new EventEmitter<any>();
    @Output() onDelete = new EventEmitter<any>();

    targetData;
    popshow;
    @Input() rightData;

    @Input() comeList;
    radioValue = 'A';

    checkChange(){

    }
    set rightText(value){
        console.log(this.rightData)
        this.rightData.text = value;
        this.onChangeTxt.emit(this.rightData);
    }
    typeChoose(event){
        console.log(this.rightData)
        this.rightData.fillData.content = event
    }
    popShow(){
        this.rightText = true;
    }

    get rightText(){
        if (!this.rightData.text) {
            return '请输入题目';
        } else {
            return this.rightData.text;
        }

    }


    constructor(public http: HttpService, public httpClient: HttpClient) { }

    ngOnInit() {
        if(this.rightData.fillData.content){
            this.radioValue = this.rightData.fillData.content
        }else{
            this.rightData.fillData.content = this.radioValue
        }
    }

    // fileChange($event) {
    //     const file: File = $event.target.files[0];
    //     const myReader: FileReader = new FileReader();
    //     const that = this;
    //     myReader.onloadend = (loadEvent: any) => {
    //         that.rightData.fillData.fillImg = loadEvent.target.result;
    //     };
    //     myReader.readAsDataURL(file);
    // }

    uploading = false;
    fileChange($event) {
        this.uploading = true;
        const file: File = $event.target.files[0];
        const myReader: FileReader = new FileReader();
        const that = this;
        myReader.onloadend = (loadEvent: any) => {
            const img = new Image();
            img.src = loadEvent.target.result;
            img.onload = function() {

            };
        };

        const formData: FormData = new FormData();
        formData.append('uploadFile', file);
        formData.append('Type', 'Image');

        this.httpClient.post(this.http.BASE_URL + '/fileUp/uploadFile', formData).subscribe(res => {
            if (res['code'] === '0') {
                myReader.readAsDataURL(file);
                const path = res['filepath'].split(`\\`).join('/')
                that.rightData.fillData.fillImg = this.http.IP_PORT + path;
                swal(`${res['msg']}`, '', 'success');
            } else {
                swal(`${res['msg']}`, '', 'info');
            }
            this.uploading = false;
        });
    }

    deleteComponent() {
        this.uploading = false;
        this.onDelete.emit();
    }

    save() {
        swal(`保存成功`, '', 'info');
    }

}
